package com.zhika.zishi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.activity.BaseActivity;
import com.zhika.zishi.activity.StrategyDetailsActivity;
import com.zhika.zishi.beans.CompareShowBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/10.
 * 对比秀的适配器
 */
public class CompareShowAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<CompareShowBean> compareShowList;

    public CompareShowAdapter(Context context,ArrayList<CompareShowBean> compareShowList){
        this.context = context;

        this.compareShowList = compareShowList;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return compareShowList.size();
    }

    @Override
    public Object getItem(int position) {
        return compareShowList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_of_compare_show,null);
            holder = new ViewHolder();
            holder.headerImageView = (ImageView) view.findViewById(R.id.item_of_compare_header_image_view);
            holder.nicknameText = (TextView) view.findViewById(R.id.item_of_compare_nickname_text);
            holder.feelText = (TextView) view.findViewById(R.id.item_of_compare_feel_text);
            holder.otherPictureImageView = (ImageView) view.findViewById(R.id.item_of_compare_other_image_view);
            holder.selfPictureImageView =(ImageView) view.findViewById(R.id.item_of_compare_self_image_view);
            holder.likeNumText = (TextView) view.findViewById(R.id.item_of_compare_like_text);
            holder.dislikeNumText =(TextView) view.findViewById(R.id.item_of_compare_dislike_text);

            holder.strategyLayout = (LinearLayout) view.findViewById(R.id.item_of_compare_strategy_layout);
            holder.strategyText = (TextView) view.findViewById(R.id.item_of_compare_strategy_text);

            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
            resetViewHolder(holder);
        }

        CompareShowBean bean = (CompareShowBean) getItem(position);

        //判断是否有攻略参考
        String strategyTitle = bean.getStrategyTitleStr();
        if (strategyTitle!=null && !strategyTitle.equals("")){
            holder.strategyLayout.setVisibility(View.VISIBLE);
            holder.strategyText.setText(strategyTitle);
            holder.strategyLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StrategyDetailsActivity.class);
                    context.startActivity(intent);
                }
            });
        }else {
            holder.strategyLayout.setVisibility(View.GONE);
        }

        //图片测试
        holder.headerImageView.setImageResource(R.drawable.ic_launcher);
        holder.nicknameText.setText(bean.getNickname());
        holder.feelText.setText(bean.getFeelStr());

        //图片测试
        holder.otherPictureImageView.setImageResource(R.drawable.test_1);
        holder.selfPictureImageView.setImageResource(R.drawable.test_3);

        holder.likeNumText.setText(bean.getLikeNumStr());
        holder.dislikeNumText.setText(bean.getDislikeNumStr());

        return view;
    }


    private void resetViewHolder(ViewHolder holder){
        holder.headerImageView.setImageResource(0);
        holder.nicknameText.setText(null);
        holder.feelText.setText(null);
        holder.otherPictureImageView.setImageResource(0);
        holder.selfPictureImageView.setImageResource(0);
        holder.likeNumText.setText(null);
        holder.dislikeNumText.setText(null);
        holder.strategyText.setText(null);
    }

    private class ViewHolder{
        public ImageView headerImageView;

        public TextView nicknameText;

        public TextView feelText;

        public ImageView otherPictureImageView;

        public ImageView selfPictureImageView;

        public TextView likeNumText;

        public TextView dislikeNumText;

        public LinearLayout strategyLayout;

        public TextView strategyText;
    }
}
